
#include <BFont>
#include <BStyle>
#include "member_BLabel.h"
#include "member_BWidget.h"

using namespace BWE;

member_BLabel::member_BLabel(BLabel* label)
{
	boss = label;
	alignment.ia_form = Form_None;
	alignment.ia_align = Align_Center;
	alignment.ia_spacing = 1;
	color.set(255, 255, 255);
	dirty = true;
	widget_member(boss)->border = 0;
}
member_BLabel::~member_BLabel()
{

}

bool member_BLabel::imageShown()
{
	if (alignment.ia_form > Form_IconAlone)
		return true;
	if (alignment.ia_form == Form_IconAlone)
	{
		if (!image)
			return false;
		return true;
	}
	if (alignment.ia_form == Form_TextAlone)
	{
		if (alignment.ia_textSize.empty() && image)
			return true;
		return false;
	}
	return image;
}

void member_BLabel::freshRects()
{
	const BRect& crect = boss->centerRect();
	const BFont* font = boss->realFont();
	const BStyle* style = boss->realStyle();
	if (style)
		style->translate(text, t_text);
	else
		t_text.clear();

	contentSize = font->size();
	if (t_text.size())
		alignment.ia_textSize = font->textSize(t_text);
	else
		alignment.ia_textSize = font->textSize(text);

	bool showImage = this->imageShown();
	if (showImage)
	{
		BSize realIconSize = imageSize;
		if (image)
		{
			if (realIconSize.width() < 0)
				realIconSize.width() = image->width();
			if (realIconSize.height() < 0)
				realIconSize.height() = image->height();
		}
		if (realIconSize.width() < 1)
			realIconSize.width() = font->size();
		if (realIconSize.height() < 1)
			realIconSize.height() = font->size();
		alignment.ia_iconSize = realIconSize;
	}
	else
	{
		alignment.ia_iconSize.reset();
	}
	alignment.adjust(BRect());
	if (contentSize.width() < alignment.ia_hintSize.width())
		contentSize.width() = alignment.ia_hintSize.width();
	if (contentSize.height() < alignment.ia_hintSize.height())
		contentSize.height() = alignment.ia_hintSize.height();
	alignment.ia_iconSize = crect.size().wrap(alignment.ia_iconSize);
	alignment.adjust(crect);
}
